#ifndef CONTIG_H
#define CONTIG_H

#include <QGraphicsWidget>
#include <QString>
#include <QGraphicsItem>
#include <QObject>
#include <QDebug>
#include <QPointer>
#include <QRectF>
#include <QPainterPath>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QHash>
#include <QColor>
#include <QTextBlockFormat>
#include <QTextCursor>

//#include "gene.h"
class BacterialContigBrowser;
class Gene;
class MovingPoint;
class GenomeSegment;

class Contig : public QGraphicsWidget
{
    Q_OBJECT

public:
    explicit Contig( QString cId, BacterialContigBrowser *parent = 0 );
    ~Contig();

    void addGene( QString geneId, int start, int stop, bool posStrand );
    void addGene( QString geneId, int start, int stop, bool posStrand, QString geneLineOfText, QString fileType );

    //setup contig location and size
    void setHeight( float newHeight );
    void setWidth( float newWidth );
    void setGenomeSegmentWidth( float newWidth );

    void setDistToMove( QPoint dtm );
    void setFlip( bool f, QPointF rotationPt );


//    void flip( QPointF rotationPt = 0 ); //flip called - can rotate about a point in terms of contig coordinates
                                        // if no input parameter will flip about the center
    void target( QPointF t ); //target location- in terms of parent coordinates
    void targetDistance( QPointF distToMove ); // target distance to move- in terms of parent coordinates

    float getHeight( );
    float getWidth( );

    int getMaxTextWidth();
    void setTextWidth( int newTextWidth);

    //these allow elts to be drawn
    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget);

    bool isInBounds(); //returns true if contig passes filter - false if not
    int getNTInContig() const;

    void layoutGenes();

    void forgeConnection( Contig* other );
    void addConnection( QString geneId, Gene* gene );

    void setLoc ( const QPointF & pos );
    void setLoc ( qreal x, qreal y );
    void setTarget( QPointF pos );

    friend bool operator< (Contig &c1, Contig &c2);
    friend bool operator<= (Contig &c1, Contig &c2);
    friend bool operator>= (Contig &c1, Contig &c2);
    friend bool operator> (Contig &c1, Contig &c2);

    //signals:

    QStringList getSelectedGenes();
    QStringList getSelectedClusters();

    bool containsCluster( QString clusterId );

public slots:
    void advance();
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);//here make sure this contig drawn on top
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);  // so label can show properly

private:
    GenomeSegment* genomeSegment;

    QString contigId;
    QList< QPointer<Gene> > genes;
    QHash< QString, QPointer<Gene> > geneHash;

    int ntInContig; //this stores how 'long' in nucleotides a contig is
                        //need this to scale all the contigs display of genes appropriately

    //movement variables
    bool updated;
    bool hovering;

    QPoint distToMove;
    QPointF rotationPt;
    bool flip;

    MovingPoint* loc;

    int widthOfText;
    int maxTextWidth;
    QGraphicsTextItem* text;
    QColor* textColor; //( 1.0, 1.0, 1.0, 1.0);
    QFont* font;
    QGraphicsRectItem* behindTextRect;

    float height;
    float width;
    bool inBounds;  //this is different from 'isVisible'
                        //isVisible tells you whether it is currently onscreen
                         //showContig tells you whether it passes the filter and should be considered
                        // in counts of genes and shown

    void setupText();

    QColor* backgroundColor;
};

#endif // CONTIG_H
